package ldh.im.admin.controller;

import java.util.Objects;
import javax.annotation.Resource;
import ldh.common.PageResult;
import ldh.common.json.JsonViewFactory;
import ldh.im.base.pojo.ImGroupUser;
import ldh.im.base.pojo.where.ImGroupUserWhere;
import ldh.im.base.service.ImGroupUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author: ldh
* @date: 2024-03-21 10:36:17
*/
@Controller
@RequestMapping("imGroupUser")
public class ImGroupUserController  {

	@Resource(name = "imGroupUserService")
	private ImGroupUserService imGroupUserService;
	

	@RequiresPermissions("imGroupUser:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public String save(@ModelAttribute ImGroupUser imGroupUser) throws Exception {
    	Objects.requireNonNull(imGroupUser);
    	if (imGroupUser.getId() == null) {
			imGroupUserService.insert(imGroupUser);
    	} else {
			imGroupUserService.updateById(imGroupUser);
    	}
    	return "redirect:/imGroupUser/list";
    }

	@RequiresPermissions("imGroupUser:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save/json")
    @ResponseBody
    public String saveJson(@ModelAttribute ImGroupUserWhere imGroupUser) throws Exception {
    	Objects.requireNonNull(imGroupUser);
    	if (imGroupUser.getId() == null) {
			imGroupUserService.insert(imGroupUser);
		} else {
			imGroupUserService.updateById(imGroupUser);
		}
        return JsonViewFactory.create()
				.toJson();
    }

	@RequiresPermissions("imGroupUser:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/toEdit/{id}")
    public String toEdit(@PathVariable Long id, Model model) throws Exception {
		ImGroupUser imGroupUser = imGroupUserService.getById(id);
    	model.addAttribute("imGroupUser", imGroupUser);
		model.addAttribute("active", "imGroupUser");
        return "imGroupUser/imGroupUserEdit";
    }

	@RequiresPermissions("imGroupUser:toAdd")
	@RequestMapping(method = RequestMethod.GET, value = "/toAdd")
    public String toAdd(Model model) throws Exception {
		model.addAttribute("active", "imGroupUser");
        return "imGroupUser/imGroupUserEdit";
    }

	@RequiresPermissions("imGroupUser:view")
	@RequestMapping(method = RequestMethod.GET, value = "/view/{id}")
    public String view(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	ImGroupUser imGroupUser = imGroupUserService.getById(id);
    	model.addAttribute("imGroupUser", imGroupUser);
		model.addAttribute("active", "imGroupUser");
        return "imGroupUser/imGroupUserView";
    }

	@RequiresPermissions("imGroupUser:edit")
	@ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/view/json/{id}")
    public String viewJson(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	ImGroupUser imGroupUser = imGroupUserService.getById(id);
    	return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", imGroupUser)
				.toJson();
    }

	@RequiresPermissions("imGroupUser:list")
	@RequestMapping(method = RequestMethod.GET, value = "/list")
	public String list(@ModelAttribute ImGroupUserWhere imGroupUserWhere, Model model) {
		imGroupUserWhere.setOrder("id desc");
		PageResult<ImGroupUser> imGroupUsers = imGroupUserService.findByImGroupUserWhere(imGroupUserWhere);
		model.addAttribute("imGroupUsers", imGroupUsers);
		model.addAttribute("active", "imGroupUser");
		model.addAttribute("imGroupUser", imGroupUserWhere);
		return "imGroupUser/imGroupUserList";
	}

	@RequiresPermissions("imGroupUser:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/list/json")
	@ResponseBody
	public String listJson(@ModelAttribute ImGroupUserWhere imGroupUserWhere, Model model) {
		imGroupUserWhere.setOrder("id desc");
		PageResult<ImGroupUser> imGroupUsers = imGroupUserService.findByImGroupUserWhere(imGroupUserWhere);
		return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", imGroupUsers)
				.toJson();
	}
	
	@RequiresPermissions("imGroupUser:delete")
	@RequestMapping(method = RequestMethod.GET, value = "/deleteById/json/{id}")
	@ResponseBody
	public String deleteJsonById(@PathVariable("id")Long id) {
		ImGroupUser imGroupUser = imGroupUserService.getById(id);
        Objects.requireNonNull(imGroupUser);
		imGroupUserService.deleteById(id);
        return JsonViewFactory.create()
				.toJson();
        }

	
}